﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc.Models;

namespace Mvc.Models
{
    [MyAuthoirize]
    public class MenuController : Controller
    {
        private UsersContext db = new UsersContext();

        //
        // GET: /Menu/

        public ActionResult Index()
        {
            return PartialView(db.Menus.ToList());
        }

        //
        // GET: /Menu/Details/5

        public ActionResult Details(int MenuId = 0)
        {
            Menu menu = db.Menus.Find(MenuId);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return PartialView(menu);
        }

        //
        // GET: /Menu/Create

        public ActionResult Create()
        {
            return PartialView();
        }

        //
        // POST: /Menu/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Menus.Add(menu);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(menu);
        }

        //
        // GET: /Menu/Edit/5

        public ActionResult Edit(int MenuId = 0)
        {
            Menu menu = db.Menus.Find(MenuId);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return PartialView(menu);
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(menu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return PartialView(menu);
        }

        //
        // GET: /Menu/Delete/5

        //public ActionResult Delete(int MenuId = 0)
        //{
        //    Menu menu = db.Menus.Find(MenuId);
        //    if (menu == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return PartialView(menu);
        //}

        //
        // POST: /Menu/Delete/5

        //[HttpPost, ActionName("Delete")]
       // [ValidateAntiForgeryToken]
        public ActionResult Delete(int MenuId)
        {
            Menu menu = db.Menus.Find(MenuId);
            db.Menus.Remove(menu);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Menu/
    [AllowAnonymous]
        public ActionResult list()
        {

            var menus = db.Menus.Include("Items");

            return PartialView(menus.ToList());
        }

        //


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}